package com.itcast.contraller;

import com.itcast.anno.LogOperationAnnotation;
import com.itcast.pojo.Dept;
import com.itcast.pojo.Result;
import com.itcast.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Slf4j
@RequestMapping("/depts")
@RestController
public class DeptController {
    //private static final Logger log = LoggerFactory.getLogger(DeptContraller.class);
    @Autowired
    private DeptService deptService;
/*
* @RequestMapping(value = "/depts",method = RequestMethod.GET)
* */
    @GetMapping
    public Result list(){
        //System.out.println("查询所有部门信息");
        log.info("查询所有部门信息");
        List<Dept> deptList = deptService.findAll();
        return Result.success(deptList);
    }
//  方式一 HttpServletRequest request
//    @DeleteMapping("/depts")
//    public Result deleteDeptById(HttpServletRequest request){
//        int id =Integer.parseInt(request.getParameter("id"));
//        System.out.println("删除部门信息"+id);
//       // deptService.deleteDeptById(id);
//        return Result.success();
//    }
    // 方式二 @RequestParam("id") Integer deptID
//    @DeleteMapping("/depts")
//    public Result deleteDeptById(@RequestParam("id") Integer deptID){
//        System.out.println("删除部门信息"+deptID);
//        // deptService.deleteDeptById(id);
//        return Result.success();
//    }
    @LogOperationAnnotation
    // 方式三 省略@RequestParam("id") 请求参数和参数名称一致 推荐
    @DeleteMapping
    public Result deleteDeptById(Integer id){
        //System.out.println("删除部门信息"+id);
        log.info("删除部门信息{}",id);
        deptService.deleteDeptById(id);
        return Result.success();
    }
    @LogOperationAnnotation
    @PostMapping
    public Result addDept(@RequestBody Dept dept){
        //System.out.println("添加部门信息"+dept);
        log.info("添加部门信息:{}",dept);
        deptService.addDept(dept);
        return Result.success();
    }
    //方式一 @PathVariable("id") Integer deptsId
//    @GetMapping("/depts/{id}")
//    public Result findDeptById(@PathVariable("id") Integer deptsId){
//        System.out.println("根据id查询部门信息"+deptsId);
//        return Result.success();
//    }

    //方式二
    @GetMapping("/{id}")
    public Result findDeptById(@PathVariable Integer id){
        //System.out.println("根据id查询部门信息"+id);
        log.info("根据id查询部门信息{}",id);
        Dept dept  = deptService.findDeptById(id);
        return Result.success(dept);
    }
    @LogOperationAnnotation
    @PutMapping
    public  Result updateDeptById(@RequestBody Dept dept){
        //System.out.println("修改部门信息"+dept.getId());
        log.info("修改部门信息{}",dept);
        deptService.updateDeptById(dept);
        return Result.success();
    }
}
